package com.yand.api.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * luna 日期转换类
 *  基于jdk1.8
 *
 */
public class DateUtils {

    /**
     * 获取当前日期的年月日的形式 yyyy-MM-dd
     * @return
     */
     public static String localDate(){
         LocalDate date = LocalDate.now();
         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
         return date.format(formatter);
     }


    /**
     * 获取当前日期的年月日的形式 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String localDateTime(){
        LocalDateTime date = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return date.format(formatter);
    }

    /**
     * 将制定日期转化成字符串
     * @param date
     * @param pattern
     * @return
     */
    public static String parseDate(Date date, String pattern){
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(formatter);
    }

    /**
     * 将指定的字符串转化成日期
     * @param dateStr
     * @param pattern
     * @return
     */
    public static Date formatDateStr(String dateStr, String pattern){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDate localDate = LocalDate.parse(dateStr, formatter);
        Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        return  Date.from(instant);

    }





}
